#!/usr/bin/env node
const { execSync } = require('child_process');
const path = require('path');

const ACT_CACHE_PATH = path.join(__dirname, '.cache/act');
const CACHE_SERVER_PATH = path.join(__dirname, '.cache/actcache');
const repos = [
  `kongxiangyiren/gitea-tool-cache@v5=${path.join(
    __dirname,
    './git/kongxiangyiren-gitea-tool-cache@v5'
  )}`,
  `actions/setup-node@v4=${path.join(__dirname, './git/actions-setup-node@v4')}`
];

let x = '';
for (const repo of repos) {
  x += `--local-repository ${repo} `;
}

const ACT_REPO = repos.length > 0 ? x : '';

const cmd = `chcp 65001 && nvs add 20.19.4 && nvs use 20.19.4 && ${path.join(
  __dirname,
  './act.exe'
)} --action-cache-path "${ACT_CACHE_PATH}" --cache-server-path "${CACHE_SERVER_PATH}" --pull=false --rebuild=false -P windows-latest=-self-hosted -P ubuntu-latest=docker.gitea.com/runner-images:ubuntu-latest ${ACT_REPO} --env-file "${path.join(
  __dirname,
  '.env'
)}"`;
console.log(cmd);

// if (!existsSync(path.join(__dirname, './act.bat'))) {
//   writeFileSync(path.join(__dirname, './act.bat'), cmd);
// }

try {
  execSync(cmd, {
    stdio: 'inherit'
  });
} catch (error) {}
